/* ajax global function */
$(document).ajaxError(function ajaxErrorHandler(event, xhr, ajaxOptions, thrownError) {
	
	//console.log('[jquery.handler.js] ajaxError!');
	
	$.unblockUI();
	GLOBAL_AJAX_EXCEPTION_STATUSCODE = xhr.status;
	//console.log('GLOBAL_AJAX_EXCEPTION_STATUSCODE : ' + GLOBAL_AJAX_EXCEPTION_STATUSCODE);

	/* Interceptor 에서 not Session 에 걸린 경우
		- xhr.status :9999,  xhr.statusText: OK, ajaxOptions:Object, thrownError:OK
		그외 Exception
		- xhr.status:500,  xhr.statusText: Internal Server Error, ajaxOptions:Object, thrownError:Internal Server Error
	*/
	
	/*
	console.log(xhr);
	console.log(ajaxOptions);
	console.log(thrownError);
	*/
	
	if(xhr.status == '9999'){
		// 인터셉터에서 보낸 필수인 세션이 없다는 에러 코드 Code(9999)이면 로그인 화면으로 보낸다.
		if(window!= window.top){
			parent.document.location.href= '/login/loginForm.do';
			self.close();
		}else {
			location.href = '/login/loginForm.do';
		}
		return false;
	}else{
		// 그외 에러코드 
		var eleHtml = [], h= -1;
		//alert('[jquery.handler.js] ajaxError start \n xhr.status : ' + xhr.status + '\n xhr.statusText :' + xhr.statusText);
		
		eleHtml[++h] = "<div class='exception-head'>";
		eleHtml[++h] = "	<h1>Exception</h1>";
		eleHtml[++h] = "	<img src='/images/common/error/logo_error.png' />";
		eleHtml[++h] = "</div>";
		eleHtml[++h] = "<hr noshade='noshade' size='1' color='#dedede' />";
		eleHtml[++h] = "<div class='exception-message'>";
		eleHtml[++h] = "	<img src='/images/app/common/ico_tree_help.gif' />";
		eleHtml[++h] = "	<span>";
		eleHtml[++h] = "		Error Code : "+xhr.status+"<br/>Error Message : "+xhr.statusText+ " ";
		eleHtml[++h] = "		<p>데이터 처리중에 오류가 발생하였습니다.</p>";
		eleHtml[++h] = "	</span>";
		eleHtml[++h] = "</div>";
		eleHtml[++h] = "<div class='exception-comment'>* 해당 에러내용은 1분안에 사라집니다.</div>";
		eleHtml[++h] = "<div class='exception-bottom'>";
		eleHtml[++h] = "	<img class='btnClose' src='/images/app/common/btn_close_m1.gif' />";
		eleHtml[++h] = "</div>";

		$.blockUI({ message: eleHtml.join('')
						, css : {width: '500px'
									, height:'180px'
									, border: 'none'
									, padding: '40px'
									, top: '30%'
									, left: '30%'
									, '-webkit-border-radius': '5px'
							        , '-moz-border-radius': '5px'
							        , cursor: 'default'
						}
						, timeout:10000});
		$('.blockUI img.btnClose').click($.unblockUI); 
		
		//return false;
	}
	//return true;
});

$(document).ajaxStart(function() {
	//  alert('[jquery.handler.js] ajaxStart!');
	GLOBAL_AJAX_EXCEPTION_STATUSCODE = '';
	$.blockUI({
			message: '<img src="/images/common/bx/bu_loading.gif"/>' 
			, overlayCSS: { background: 'transparent'}
			, css:{background: 'transparent', border:'none'}
	});
});

var GLOBAL_AJAX_EXCEPTION_STATUSCODE = '';

$(document).ajaxStop(function() {
//	console.log('[jquery.handler.js] ajaxStop!');
	if(GLOBAL_AJAX_EXCEPTION_STATUSCODE == '') {
		$.unblockUI();
	}
});

